#include <arduino.h>
#include <Wire.h>
#include "DHT.h"
#include "Adafruit_SGP30.h"

#define DHTPIN  2   
#define DHTTYPE DHT11

//用于存放温湿度；
float humidity;
float temperature;
float eTVOC;
float eCO2;

Adafruit_SGP30 sgp;     //CO2的sda在GPIO4,scl在GPIO5;

DHT dht(DHTPIN, DHTTYPE);  //温湿度在GPIO1;

//设定CO2传感器的温湿度；
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
    const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature));
    const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); 
    return absoluteHumidityScaled;
}
 
void setup() {
  Serial.begin(9600);
  dht.begin();
  sgp.begin();
}

void loop() {
  
  humidity = dht.readHumidity();//读湿度
  temperature = dht.readTemperature();//读温度(摄氏度)
  
  sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
  
  eTVOC = sgp.TVOC;
  eCO2 = sgp.eCO2;

//串口输出传感器浓度；
  Serial.print("TVOC:"); 
  Serial.print(eTVOC); 
  Serial.println(" ppb");
  
  Serial.print("eCO2:"); 
  Serial.print(eCO2); 
  Serial.println(" ppm");
  
  Serial.print("湿度是：");
  Serial.print(humidity);
  Serial.println(" %RH");
  
  Serial.print("温度是：");
  Serial.print(temperature);
  Serial.println(" ℃");
  Serial.println("\n");
  
  delay(10000);

}
